﻿using System.Data;
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls; 
using System.Windows.Input;
using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Input;
using WPFExcelReport.Helpers;
using System.Windows.Controls;
namespace WPFExcelReport
{
    public partial class UIEXORD : ViewBase
    {
        public int pos = 1;
        string user = "";
        public bool ModeEdit;
        TDRAUFTR cDR1
        {
            get
            {
                return (TDRAUFTR)this.FindName("cDR1");
            }
        }
        TDRAUFTR cDR2
        {
            get
            {
                return (TDRAUFTR)this.FindName("cDR2");
            }
        }
        TDRAUFTR cDR3
        {
            get
            {
                return (TDRAUFTR)this.FindName("cDR3");
            }
        }
        public void ResetSeq(object sender, RoutedEventArgs e)
        {
            if (!TMV.IsITX)
            {
                return;
            } 
        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return; 
            base.Window_Loaded(sender, e);
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            {
                KEY = "";

            }
            else
            {
                KEY = TMV.VKPAUFKRS.PadRight(2)+ TMV.VKPAUFLFD.PadRight(6) + TMV.VKPAUFPOS.PadRight(3);
                VAR = TMV.CUSTOMER;
            }
        }
        public void txtNArt_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key != System.Windows.Input.Key.Enter)
                    return;
                this.Focus();
                TextBox txt = sender as TextBox;
                PM["USD2RMB"] = USD2RMB;
                PM["RATE1"] = RATE1;
                if (ART.Trim().Length > 20)
                {
                    PM["VFART"] = ART.Trim().Substring(0, 20);
                    PM["VKPARTDES"] = ART.Trim().Substring(20);
                }
                else
                {
                    PM["VFART"] = ART.Trim();
                    PM["VKPARTDES"] = "";
                }
                PM["VCOLOR"] = TMV.VKPFARBE;
                PM["REMARK"] = TMV.REMARK;
                PM["PRICE"] = TMV.PRICE;
                PM["NUMBER1"] = TMV.NUMBER1;
                PM["NUMBER2"] = TMV.NUMBER2;
                PM["dwVKP"] = dwMaster;
                TMV.SETVKPBYART(PM);
                txt.Focus();
                vwMaster.View.MoveCurrentTo(-1);
                vwMaster.View.MoveCurrentToFirst();
                if (ModeEdit)
                    return;
                NewVKP(dwMaster); 
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace, ex.Message);
            }
        }
        public void NewVKP(DataRow dwInput)
        {
            PM["dwInput"] = dwInput;
            PM["KRS"] =  app.TERMNO.PadRight(2).Substring(0,2);
            PM["LFD"] = Helpers.Helper.Right("000000" + MAXORD, 6);
            PM["POS"] = Helpers.Helper.Right("000" + (pos++).ToString(), 3);
            PM["CUSTOMER"] =  VAR.PadRight(40).Substring(0, 40);
            PM["CUSER"] = app.User;
            PM["USD2RMB"] = USD2RMB; 
            PM["RATE1"] = RATE1;
            TMV.NEWVKP(PM);
            vwMaster.View.MoveCurrentToFirst();
        }

        public void PrintCommand1(object sender, RoutedEventArgs e)
        {           
            if (dwMaster == null)
                return;         
            DataSet Set = new DataSet();
            filter = "VKPART not like 'PleaseEnter%' AND VKPAUFKRS='" + dwMaster["VKPAUFKRS"].ToString() + "' AND VKPAUFLFD='" + dwMaster["VKPAUFLFD"].ToString() + "'";
            sql = "DELETE FROM ICC_EXORD WHERE VKPART like 'PleaseEnter%' AND VKPAUFKRS='" + dwMaster["VKPAUFKRS"].ToString() + "' AND VKPAUFLFD='" + dwMaster["VKPAUFLFD"].ToString() + "'";
            TMV.SQLiteDatabase.ExecuteNonQuery(sql);
            rows = TBMain.Select(filter);
            DataTable TB = rows.CopyToDataTable();           
            TB.TableName = TMV.TableName;
            Set.Merge(TB);
            cDR1.PRINT(Set);
        }
        public void PrintCommand2(object sender, RoutedEventArgs e)
        {           
            if (dwMaster == null)
                return;
            DataSet Set = new DataSet();
            filter = "VKPART not like 'PleaseEnter%' AND VKPAUFKRS='" + dwMaster["VKPAUFKRS"].ToString() + "' AND VKPAUFLFD='" + dwMaster["VKPAUFLFD"].ToString() + "'";
            sql = "DELETE FROM ICC_EXORD WHERE VKPART like 'PleaseEnter%' AND VKPAUFKRS='" + dwMaster["VKPAUFKRS"].ToString() + "' AND VKPAUFLFD='" + dwMaster["VKPAUFLFD"].ToString() + "'";
            TMV.SQLiteDatabase.ExecuteNonQuery(sql);
            rows = TBMain.Select(filter);
            DataTable TB = rows.CopyToDataTable();
            TB.TableName = TMV.TableName;
            Set.Merge(TB);
            cDR2.PRINT(Set);
        }
        public void PrintCommand3(object sender, RoutedEventArgs e)
        {           
            if (dwMaster == null)
                return;
            DataSet Set = new DataSet();
            filter = "VKPART not like 'PleaseEnter%' AND VKPAUFKRS='" + dwMaster["VKPAUFKRS"].ToString() + "' AND VKPAUFLFD='" + dwMaster["VKPAUFLFD"].ToString() + "'";
            sql = "DELETE FROM ICC_EXORD WHERE VKPART like 'PleaseEnter%' AND VKPAUFKRS='" + dwMaster["VKPAUFKRS"].ToString() + "' AND VKPAUFLFD='" + dwMaster["VKPAUFLFD"].ToString() + "'";
            TMV.SQLiteDatabase.ExecuteNonQuery(sql);
            rows = TBMain.Select(filter);
            DataTable TB = rows.CopyToDataTable();
            TB.TableName = TMV.TableName;
            Set.Merge(TB);
            cDR3.PRINT(Set);
        }
        public string sql = "";
        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            if (dwMaster["VKPART"].ToString() == "PleaseEnter")
                return;
            string id = dwMaster["ROWNID"].ToString();
            sql = "DELETE FROM ICC_EXORD WHERE ROWNID = '" + id + "' ";
            TMV.SQLiteDatabase.ExecuteNonQuery(sql);
            filter = "ROWNID = '" + id + "' ";
            rows = TBMain.Select(filter);
            vwMaster.View.MoveCurrentTo(-1);
            foreach (DataRow row in rows)
            {
                row.Delete();
            }
            vwMaster.View.MoveCurrentToFirst();
            base.DeleteCommand(sender, e);
        }
    }
    public partial class UIEXORD
    {
        public VICC_EXORD TMV
        {
            get
            {
                return (VICC_EXORD)MV;
            }
        }
        #region ==MAXORD===========
        public static readonly DependencyProperty MAXORDProperty = DependencyProperty.Register("MAXORD",
            typeof(int), typeof(UIEXORD),
            new FrameworkPropertyMetadata(1, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnMAXORDChanged));
        public int MAXORD
        {
            get
            {
                return (int)GetValue(MAXORDProperty);
            }
            set
            {
                SetValue(MAXORDProperty, value);
                OnMAXORDChg();
            }
        }
        private static void OnMAXORDChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIEXORD;
            if (src != null)
            {
                src.MAXORD = (int)e.NewValue;
            }
        }
        public virtual void OnMAXORDChg()
        {

        }
        #endregion
        #region ==USD2RMB===========
        public static readonly DependencyProperty USD2RMBProperty = DependencyProperty.Register("USD2RMB",
            typeof(double), typeof(UIEXORD),
            new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnUSD2RMBChanged));
        public double USD2RMB
        {
            get
            {
                return (double)GetValue(USD2RMBProperty);
            }
            set
            {
                SetValue(USD2RMBProperty, value);
                OnUSD2RMBChg();
            }
        }
        private static void OnUSD2RMBChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIEXORD;
            if (src != null)
            {
                src.USD2RMB = (double)e.NewValue;
            }
        }
        public virtual void OnUSD2RMBChg()
        {
            
        }
        #endregion
        #region ==RATE1===========
        public static readonly DependencyProperty RATE1Property = DependencyProperty.Register("RATE1",
            typeof(double), typeof(UIEXORD),
            new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnRATE1Changed));
        public double RATE1
        {
            get
            {
                return (double)GetValue(RATE1Property);
            }
            set
            {
                SetValue(RATE1Property, value);
                OnRATE1Chg();
            }
        }
        private static void OnRATE1Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIEXORD;
            if (src != null)
            {
                src.RATE1 = (double)e.NewValue;
            }
        }
        public virtual void OnRATE1Chg()
        {
           
        }
        #endregion


    }
}
